#include "beginhall.h"
#include<QIcon>
#include<QPushButton>
#include<QPainter>
#include<QList>
#include<QRectF>
#include<QTimer>
BeginHall::BeginHall(QWidget *parent) : QWidget(parent)
{
    qsrand(time(nullptr));
    this->setFixedSize(1000,800);
    this->setWindowIcon(QIcon(":rec/sheng.png"));
    this->setWindowTitle("游戏运行");
    snackList.push_back(QRectF(this->width()/2,this->height()/2,snackWidth,snackHeight));
    moveUp();
    moveUp();
    creatFoot();
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,[=](){
        auto headNode = snackList.front();
        int cnt = 1;
        if(headNode == footRect)
        {
            cnt++;
            create();
        }
        while(cnt--)
        {
            switch(derection)
            {
              case Derection::UP:moveUp();
                break;
            case Derection::DOWN:moveDown();
                break;
            case Derection::LEFT:moveLeft();
                break;
            case Derection::RIGHT:moveRight();
                break;
            }
        }
        snackList.pop_back();
        update();

    });


    QPushButton*startButton = new QPushButton(this);
    QPushButton*stopButton = new QPushButton(this);
    startButton->move(850,160);
    stopButton->move(850,200);
}

void BeginHall::paintEvent(QPaintEvent *event)
{
    QPainter*painter = new QPainter(this);
    QPixmap pixmap;
    pixmap.load(":rec/game.jpg");
    painter->drawPixmap(0,0,800,800,pixmap);
    pixmap.load(":rec/operator.jpg");
    painter->drawPixmap(800,0,200,800,pixmap);
    if(derection == Derection::UP)
    {
        pixmap.load(":rec/up.png");
    }
    else if(derection == Derection::DOWN)
    {
        pixmap.load(":rec/down.png");
    }
    else if(derection == Derection::LEFT)
    {
        pixmap.load(":rec/left.png");
    }
    else
    {
        pixmap.load(":rec/right.png");
    }
    QRectF headNode = snackList.front();
    painter->drawPixmap(headNode.x(),headNode.y(),snackWidth,snackHeight,pixmap);
    pixmap.load(":rec/shen.png");
    for(int i = 1;i<snackList.size();i++)
    {
         painter->drawPixmap(snackList[i].x(),snackList[i].y(),snackWidth,snackHeight,pixmap);
    }

    //绘画食物；
    pixmap.load(":rec/shen.png");
    painter->drawPixmap(footRect.x(),footRect.y(),snackWidth,snackHeight,pixmap);


}

void BeginHall::moveUp()
{
    QPoint topLeft;
    QPoint bottomRight;
    QRectF headNode = snackList.front();
    int headX = headNode.x();
    int headY = headNode.y();
    if(headY < 0)
    {
        topLeft = QPoint(headX,this->height() - snackHeight);
    }
    else
    {
        topLeft = QPoint(headX,headY - snackHeight);
    }
    bottomRight = topLeft + QPoint(snackWidth,snackHeight);
    snackList.push_front(QRectF(topLeft,bottomRight));
}

void BeginHall::moveDown()
{
    QPoint topLeft;
    QPoint bottomRight;
    QRectF headNode = snackList.front();
    int headX = headNode.x();
    int headY = headNode.y();
    if(headY > this->height())
    {
        topLeft = QPoint(headX,0);
    }
    else
    {
        topLeft = QPoint(headX,headY + snackHeight);
    }
    bottomRight = topLeft + QPoint(snackWidth,snackHeight);
    QRectF rect(topLeft,bottomRight);
    snackList.push_front(rect);
}

void BeginHall::moveLeft()
{
    QPoint topLeft;
    QPoint bottomRight;
    QRectF headNode = snackList.front();
    int headX = headNode.x();
    int headY = headNode.y();
    if(headX < 0)
    {
        topLeft = QPoint(800 - snackWidth,headY);
    }
    else
    {
        topLeft = QPoint(headX - snackWidth,headY);
    }
    bottomRight = topLeft + QPoint(snackWidth,snackHeight);
    snackList.push_front(QRectF(topLeft,bottomRight));
}

void BeginHall::moveRight()
{
    QPoint topLeft;
    QPoint bottomRight;
    QRectF headNode = snackList.front();
    int headX = headNode.x();
    int headY = headNode.y();
    if(headX > 800)
    {
        topLeft = QPoint(0,headY);
    }
    else
    {
        topLeft = QPoint(headX +snackWidth,headY);
    }
    bottomRight = topLeft + QPoint(snackWidth,snackHeight);
    snackList.push_front(QRectF(topLeft,bottomRight));
}


bool BeginHall::chechFail()
{
    for(int i = 0;i<snackList.size();i++)
    {
        for(int j = i+1;j<snackList.size();j++)
        {
            if(snackList[j] == snackList[i])//  相撞游戏结束；
            {
                return true;
            }
        }

    }
    return false;
}

void BeginHall::creatFoot()
{
    footRect =QRectF(qrand()/800,qrand()/800,snackWidth,snackHeight);
}
